package com.auxgroup.hr.front.site.co.common;

import java.util.Map;

import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.utils.WebServiceClient;
import com.auxgroup.hr.front.commons.utils.WebServiceUtils;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCPyIdtfCode;
import com.auxgroup.hr.front.dao.repository.PsCPyIdtfCodeRepository;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.ws.SoapXmlBuild;

/**
 * 系统接口
 * 
 * @author wangchangyun
 * @version 创建时间：180527
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/common")
public class CommonCO extends BaseCO {
	
	@Autowired
	private PsCPyIdtfCodeRepository psCPyIdtfCodeRepository;

	
	@Value("${ws.processService.approve.address}")
	private String processURL;
	
	@Value("${ws.processService.sendMsg.soapAction}")
	private String sendMsgSoapAction;

	/**
	 * 获取用户信息
	 * @return
	 */
	@GetMapping(value = "/getUser")
	public BaseResponseVO getOne(@SessionGain("user") UserDTO user) {
		return buildRespone(user,false);
	}
	
	/**
	 * 发送短信验证码
	 * @param user
	 * @param mobileNum
	 * @return
	 */
	@PostMapping(value="/sendMsg")
	public BaseResponseVO sendMsg(String mobileNum,@SessionGain("user") UserDTO user){
		logger.info("send msg to mobile:{}",mobileNum);
		if(StringUtils.isBlank(mobileNum)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"手机号不能为空");
		}
		StringBuilder soapXml = new StringBuilder();
		SoapXmlBuild.buildSendMobildMsg(soapXml,user.getEmplId(),mobileNum);
		String responXml = WebServiceClient.doPostSoap(processURL, soapXml.toString(), sendMsgSoapAction);
		Map<String, String> result = WebServiceUtils.handleSendMsgRespXml(responXml,"C_MESSAGE_RES");
		if(result!=null&&"成功".equals(result.get("MESSAGE_TEXT"))){
			return buildRespone(BaseRetCode.CODE_SUCCESS);
		}
		return buildRespone(BaseRetCode.CODE_ERROR_MOBILE_SEND_FAILD);
	}
	
	/**
	 * 验证短信验证码
	 * @param emplid
	 * @param checkCode
	 * @return
	 */
	@PostMapping(value="/checkCode")
	public BaseResponseVO checkCode(String checkCode,@SessionGain("user") UserDTO user){
		if(StringUtils.isBlank(checkCode)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		//验证码是否存在
		PsCPyIdtfCode tfcode = psCPyIdtfCodeRepository.findOne(user.getEmplId());
		if(tfcode==null){
			return buildRespone(BaseRetCode.CODE_ERROR_CHECKCODE_NOT_EXISTS);
		}
		//验证码是否已过期
		Long sendTime = tfcode.getLastupddttm().getTime();
		Long intervalTime = System.currentTimeMillis()-sendTime;
		if(intervalTime/60000>=5){
			return buildRespone(BaseRetCode.CODE_ERROR_CHECKCODE_HAX_EXPIRED);
		}
		//验证码是否正确
		if(!checkCode.equals(tfcode.getCIdtfCode())){
			return buildRespone(BaseRetCode.CODE_ERROR_CHECKCODE_NOT_RIGHT);
		}
		return buildRespone(BaseRetCode.CODE_SUCCESS);
	}
}
